{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src='https://static.wixstatic.com/media/f2e08c1ae4fb5a1d24933a57f5e012d8.png/v1/fill/w_79,h_81,al_c,usm_0.66_1.00_0.01/f2e08c1ae4fb5a1d24933a57f5e012d8.png' /></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Predicting credit default"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data=pd.read_csv('F:/course/Logistic regression/credit_training.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>SeriousDlqin2yrs</th>\n",
       "      <th>RevolvingUtilizationOfUnsecuredLines</th>\n",
       "      <th>age</th>\n",
       "      <th>NumberOfTime30to59DaysPastDueNotWorse</th>\n",
       "      <th>DebtRatio</th>\n",
       "      <th>MonthlyIncome</th>\n",
       "      <th>NumberOfOpenCreditLinesAndLoans</th>\n",
       "      <th>NumberOfTimes90DaysLate</th>\n",
       "      <th>NumberRealEstateLoansOrLines</th>\n",
       "      <th>NumberOfTime60to89DaysPastDueNotWorse</th>\n",
       "      <th>NumberOfDependents</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.766127</td>\n",
       "      <td>45</td>\n",
       "      <td>2</td>\n",
       "      <td>0.802982</td>\n",
       "      <td>9120.0</td>\n",
       "      <td>13</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0.957151</td>\n",
       "      <td>40</td>\n",
       "      <td>0</td>\n",
       "      <td>0.121876</td>\n",
       "      <td>2600.0</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0.658180</td>\n",
       "      <td>38</td>\n",
       "      <td>1</td>\n",
       "      <td>0.085113</td>\n",
       "      <td>3042.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>0.233810</td>\n",
       "      <td>30</td>\n",
       "      <td>0</td>\n",
       "      <td>0.036050</td>\n",
       "      <td>3300.0</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0.907239</td>\n",
       "      <td>49</td>\n",
       "      <td>1</td>\n",
       "      <td>0.024926</td>\n",
       "      <td>63588.0</td>\n",
       "      <td>7</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Unnamed: 0  SeriousDlqin2yrs  RevolvingUtilizationOfUnsecuredLines  age  \\\n",
       "0           1                 1                              0.766127   45   \n",
       "1           2                 0                              0.957151   40   \n",
       "2           3                 0                              0.658180   38   \n",
       "3           4                 0                              0.233810   30   \n",
       "4           5                 0                              0.907239   49   \n",
       "\n",
       "   NumberOfTime30to59DaysPastDueNotWorse  DebtRatio  MonthlyIncome  \\\n",
       "0                                      2   0.802982         9120.0   \n",
       "1                                      0   0.121876         2600.0   \n",
       "2                                      1   0.085113         3042.0   \n",
       "3                                      0   0.036050         3300.0   \n",
       "4                                      1   0.024926        63588.0   \n",
       "\n",
       "   NumberOfOpenCreditLinesAndLoans  NumberOfTimes90DaysLate  \\\n",
       "0                               13                        0   \n",
       "1                                4                        0   \n",
       "2                                2                        1   \n",
       "3                                5                        0   \n",
       "4                                7                        0   \n",
       "\n",
       "   NumberRealEstateLoansOrLines  NumberOfTime60to89DaysPastDueNotWorse  \\\n",
       "0                             6                                      0   \n",
       "1                             0                                      0   \n",
       "2                             0                                      0   \n",
       "3                             0                                      0   \n",
       "4                             1                                      0   \n",
       "\n",
       "   NumberOfDependents  \n",
       "0                 2.0  \n",
       "1                 1.0  \n",
       "2                 0.0  \n",
       "3                 0.0  \n",
       "4                 0.0  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# seems like the first column is not needed\n",
    "data=data.drop(['Unnamed: 0'],axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SeriousDlqin2yrs</th>\n",
       "      <th>RevolvingUtilizationOfUnsecuredLines</th>\n",
       "      <th>age</th>\n",
       "      <th>NumberOfTime30to59DaysPastDueNotWorse</th>\n",
       "      <th>DebtRatio</th>\n",
       "      <th>MonthlyIncome</th>\n",
       "      <th>NumberOfOpenCreditLinesAndLoans</th>\n",
       "      <th>NumberOfTimes90DaysLate</th>\n",
       "      <th>NumberRealEstateLoansOrLines</th>\n",
       "      <th>NumberOfTime60to89DaysPastDueNotWorse</th>\n",
       "      <th>NumberOfDependents</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.766127</td>\n",
       "      <td>45</td>\n",
       "      <td>2</td>\n",
       "      <td>0.802982</td>\n",
       "      <td>9120.0</td>\n",
       "      <td>13</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>0.957151</td>\n",
       "      <td>40</td>\n",
       "      <td>0</td>\n",
       "      <td>0.121876</td>\n",
       "      <td>2600.0</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0.658180</td>\n",
       "      <td>38</td>\n",
       "      <td>1</td>\n",
       "      <td>0.085113</td>\n",
       "      <td>3042.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>0.233810</td>\n",
       "      <td>30</td>\n",
       "      <td>0</td>\n",
       "      <td>0.036050</td>\n",
       "      <td>3300.0</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0.907239</td>\n",
       "      <td>49</td>\n",
       "      <td>1</td>\n",
       "      <td>0.024926</td>\n",
       "      <td>63588.0</td>\n",
       "      <td>7</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   SeriousDlqin2yrs  RevolvingUtilizationOfUnsecuredLines  age  \\\n",
       "0                 1                              0.766127   45   \n",
       "1                 0                              0.957151   40   \n",
       "2                 0                              0.658180   38   \n",
       "3                 0                              0.233810   30   \n",
       "4                 0                              0.907239   49   \n",
       "\n",
       "   NumberOfTime30to59DaysPastDueNotWorse  DebtRatio  MonthlyIncome  \\\n",
       "0                                      2   0.802982         9120.0   \n",
       "1                                      0   0.121876         2600.0   \n",
       "2                                      1   0.085113         3042.0   \n",
       "3                                      0   0.036050         3300.0   \n",
       "4                                      1   0.024926        63588.0   \n",
       "\n",
       "   NumberOfOpenCreditLinesAndLoans  NumberOfTimes90DaysLate  \\\n",
       "0                               13                        0   \n",
       "1                                4                        0   \n",
       "2                                2                        1   \n",
       "3                                5                        0   \n",
       "4                                7                        0   \n",
       "\n",
       "   NumberRealEstateLoansOrLines  NumberOfTime60to89DaysPastDueNotWorse  \\\n",
       "0                             6                                      0   \n",
       "1                             0                                      0   \n",
       "2                             0                                      0   \n",
       "3                             0                                      0   \n",
       "4                             1                                      0   \n",
       "\n",
       "   NumberOfDependents  \n",
       "0                 2.0  \n",
       "1                 1.0  \n",
       "2                 0.0  \n",
       "3                 0.0  \n",
       "4                 0.0  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Admin\\Anaconda4\\lib\\site-packages\\numpy\\lib\\function_base.py:3834: RuntimeWarning: Invalid value encountered in percentile\n",
      "  RuntimeWarning)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SeriousDlqin2yrs</th>\n",
       "      <th>RevolvingUtilizationOfUnsecuredLines</th>\n",
       "      <th>age</th>\n",
       "      <th>NumberOfTime30to59DaysPastDueNotWorse</th>\n",
       "      <th>DebtRatio</th>\n",
       "      <th>MonthlyIncome</th>\n",
       "      <th>NumberOfOpenCreditLinesAndLoans</th>\n",
       "      <th>NumberOfTimes90DaysLate</th>\n",
       "      <th>NumberRealEstateLoansOrLines</th>\n",
       "      <th>NumberOfTime60to89DaysPastDueNotWorse</th>\n",
       "      <th>NumberOfDependents</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>1.202690e+05</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>146076.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>0.066840</td>\n",
       "      <td>6.048438</td>\n",
       "      <td>52.295207</td>\n",
       "      <td>0.421033</td>\n",
       "      <td>353.005076</td>\n",
       "      <td>6.670221e+03</td>\n",
       "      <td>8.452760</td>\n",
       "      <td>0.265973</td>\n",
       "      <td>1.018240</td>\n",
       "      <td>0.240387</td>\n",
       "      <td>0.757222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.249746</td>\n",
       "      <td>249.755371</td>\n",
       "      <td>14.771866</td>\n",
       "      <td>4.192781</td>\n",
       "      <td>2037.818523</td>\n",
       "      <td>1.438467e+04</td>\n",
       "      <td>5.145951</td>\n",
       "      <td>4.169304</td>\n",
       "      <td>1.129771</td>\n",
       "      <td>4.155179</td>\n",
       "      <td>1.115086</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.029867</td>\n",
       "      <td>41.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.175074</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.154181</td>\n",
       "      <td>52.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.366508</td>\n",
       "      <td>NaN</td>\n",
       "      <td>8.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.559046</td>\n",
       "      <td>63.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.868254</td>\n",
       "      <td>NaN</td>\n",
       "      <td>11.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>50708.000000</td>\n",
       "      <td>109.000000</td>\n",
       "      <td>98.000000</td>\n",
       "      <td>329664.000000</td>\n",
       "      <td>3.008750e+06</td>\n",
       "      <td>58.000000</td>\n",
       "      <td>98.000000</td>\n",
       "      <td>54.000000</td>\n",
       "      <td>98.000000</td>\n",
       "      <td>20.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       SeriousDlqin2yrs  RevolvingUtilizationOfUnsecuredLines            age  \\\n",
       "count     150000.000000                         150000.000000  150000.000000   \n",
       "mean           0.066840                              6.048438      52.295207   \n",
       "std            0.249746                            249.755371      14.771866   \n",
       "min            0.000000                              0.000000       0.000000   \n",
       "25%            0.000000                              0.029867      41.000000   \n",
       "50%            0.000000                              0.154181      52.000000   \n",
       "75%            0.000000                              0.559046      63.000000   \n",
       "max            1.000000                          50708.000000     109.000000   \n",
       "\n",
       "       NumberOfTime30to59DaysPastDueNotWorse      DebtRatio  MonthlyIncome  \\\n",
       "count                          150000.000000  150000.000000   1.202690e+05   \n",
       "mean                                0.421033     353.005076   6.670221e+03   \n",
       "std                                 4.192781    2037.818523   1.438467e+04   \n",
       "min                                 0.000000       0.000000   0.000000e+00   \n",
       "25%                                 0.000000       0.175074            NaN   \n",
       "50%                                 0.000000       0.366508            NaN   \n",
       "75%                                 0.000000       0.868254            NaN   \n",
       "max                                98.000000  329664.000000   3.008750e+06   \n",
       "\n",
       "       NumberOfOpenCreditLinesAndLoans  NumberOfTimes90DaysLate  \\\n",
       "count                    150000.000000            150000.000000   \n",
       "mean                          8.452760                 0.265973   \n",
       "std                           5.145951                 4.169304   \n",
       "min                           0.000000                 0.000000   \n",
       "25%                           5.000000                 0.000000   \n",
       "50%                           8.000000                 0.000000   \n",
       "75%                          11.000000                 0.000000   \n",
       "max                          58.000000                98.000000   \n",
       "\n",
       "       NumberRealEstateLoansOrLines  NumberOfTime60to89DaysPastDueNotWorse  \\\n",
       "count                 150000.000000                          150000.000000   \n",
       "mean                       1.018240                               0.240387   \n",
       "std                        1.129771                               4.155179   \n",
       "min                        0.000000                               0.000000   \n",
       "25%                        0.000000                               0.000000   \n",
       "50%                        1.000000                               0.000000   \n",
       "75%                        2.000000                               0.000000   \n",
       "max                       54.000000                              98.000000   \n",
       "\n",
       "       NumberOfDependents  \n",
       "count       146076.000000  \n",
       "mean             0.757222  \n",
       "std              1.115086  \n",
       "min              0.000000  \n",
       "25%                   NaN  \n",
       "50%                   NaN  \n",
       "75%                   NaN  \n",
       "max             20.000000  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Seems like monthlyincome & numberofdependents have some missing values\n",
    "# Also, there seem to be outliers across variables\n",
    "# Lets fix the missing values first & then outliers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# There are multiple ways in which we can fix missing values\n",
    "# for now, lets just impute missing values with median of that column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data['MonthlyIncome']=data['MonthlyIncome'].fillna(value=data['MonthlyIncome'].median())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    1.500000e+05\n",
       "mean     6.418455e+03\n",
       "std      1.289040e+04\n",
       "min      0.000000e+00\n",
       "25%      3.903000e+03\n",
       "50%      5.400000e+03\n",
       "75%      7.400000e+03\n",
       "max      3.008750e+06\n",
       "Name: MonthlyIncome, dtype: float64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['MonthlyIncome'].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data['NumberOfDependents']=data['NumberOfDependents'].fillna(value=data['NumberOfDependents'].median())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SeriousDlqin2yrs</th>\n",
       "      <th>RevolvingUtilizationOfUnsecuredLines</th>\n",
       "      <th>age</th>\n",
       "      <th>NumberOfTime30to59DaysPastDueNotWorse</th>\n",
       "      <th>DebtRatio</th>\n",
       "      <th>MonthlyIncome</th>\n",
       "      <th>NumberOfOpenCreditLinesAndLoans</th>\n",
       "      <th>NumberOfTimes90DaysLate</th>\n",
       "      <th>NumberRealEstateLoansOrLines</th>\n",
       "      <th>NumberOfTime60to89DaysPastDueNotWorse</th>\n",
       "      <th>NumberOfDependents</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>1.500000e+05</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>0.066840</td>\n",
       "      <td>6.048438</td>\n",
       "      <td>52.295207</td>\n",
       "      <td>0.421033</td>\n",
       "      <td>353.005076</td>\n",
       "      <td>6.418455e+03</td>\n",
       "      <td>8.452760</td>\n",
       "      <td>0.265973</td>\n",
       "      <td>1.018240</td>\n",
       "      <td>0.240387</td>\n",
       "      <td>0.737413</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.249746</td>\n",
       "      <td>249.755371</td>\n",
       "      <td>14.771866</td>\n",
       "      <td>4.192781</td>\n",
       "      <td>2037.818523</td>\n",
       "      <td>1.289040e+04</td>\n",
       "      <td>5.145951</td>\n",
       "      <td>4.169304</td>\n",
       "      <td>1.129771</td>\n",
       "      <td>4.155179</td>\n",
       "      <td>1.107021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.029867</td>\n",
       "      <td>41.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.175074</td>\n",
       "      <td>3.903000e+03</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.154181</td>\n",
       "      <td>52.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.366508</td>\n",
       "      <td>5.400000e+03</td>\n",
       "      <td>8.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.559046</td>\n",
       "      <td>63.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.868254</td>\n",
       "      <td>7.400000e+03</td>\n",
       "      <td>11.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>50708.000000</td>\n",
       "      <td>109.000000</td>\n",
       "      <td>98.000000</td>\n",
       "      <td>329664.000000</td>\n",
       "      <td>3.008750e+06</td>\n",
       "      <td>58.000000</td>\n",
       "      <td>98.000000</td>\n",
       "      <td>54.000000</td>\n",
       "      <td>98.000000</td>\n",
       "      <td>20.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       SeriousDlqin2yrs  RevolvingUtilizationOfUnsecuredLines            age  \\\n",
       "count     150000.000000                         150000.000000  150000.000000   \n",
       "mean           0.066840                              6.048438      52.295207   \n",
       "std            0.249746                            249.755371      14.771866   \n",
       "min            0.000000                              0.000000       0.000000   \n",
       "25%            0.000000                              0.029867      41.000000   \n",
       "50%            0.000000                              0.154181      52.000000   \n",
       "75%            0.000000                              0.559046      63.000000   \n",
       "max            1.000000                          50708.000000     109.000000   \n",
       "\n",
       "       NumberOfTime30to59DaysPastDueNotWorse      DebtRatio  MonthlyIncome  \\\n",
       "count                          150000.000000  150000.000000   1.500000e+05   \n",
       "mean                                0.421033     353.005076   6.418455e+03   \n",
       "std                                 4.192781    2037.818523   1.289040e+04   \n",
       "min                                 0.000000       0.000000   0.000000e+00   \n",
       "25%                                 0.000000       0.175074   3.903000e+03   \n",
       "50%                                 0.000000       0.366508   5.400000e+03   \n",
       "75%                                 0.000000       0.868254   7.400000e+03   \n",
       "max                                98.000000  329664.000000   3.008750e+06   \n",
       "\n",
       "       NumberOfOpenCreditLinesAndLoans  NumberOfTimes90DaysLate  \\\n",
       "count                    150000.000000            150000.000000   \n",
       "mean                          8.452760                 0.265973   \n",
       "std                           5.145951                 4.169304   \n",
       "min                           0.000000                 0.000000   \n",
       "25%                           5.000000                 0.000000   \n",
       "50%                           8.000000                 0.000000   \n",
       "75%                          11.000000                 0.000000   \n",
       "max                          58.000000                98.000000   \n",
       "\n",
       "       NumberRealEstateLoansOrLines  NumberOfTime60to89DaysPastDueNotWorse  \\\n",
       "count                 150000.000000                          150000.000000   \n",
       "mean                       1.018240                               0.240387   \n",
       "std                        1.129771                               4.155179   \n",
       "min                        0.000000                               0.000000   \n",
       "25%                        0.000000                               0.000000   \n",
       "50%                        1.000000                               0.000000   \n",
       "75%                        2.000000                               0.000000   \n",
       "max                       54.000000                              98.000000   \n",
       "\n",
       "       NumberOfDependents  \n",
       "count       150000.000000  \n",
       "mean             0.737413  \n",
       "std              1.107021  \n",
       "min              0.000000  \n",
       "25%              0.000000  \n",
       "50%              0.000000  \n",
       "75%              1.000000  \n",
       "max             20.000000  "
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "vars=[\"RevolvingUtilizationOfUnsecuredLines\",\"NumberOfTime30to59DaysPastDueNotWorse\",\"DebtRatio\",\"MonthlyIncome\",\"NumberOfTimes90DaysLate\",\"NumberRealEstateLoansOrLines\",\"NumberOfTime60to89DaysPastDueNotWorse\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.99999989999999994"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t=\"RevolvingUtilizationOfUnsecuredLines\"\n",
    "data[t].quantile(0.95)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "x=data[t].quantile(0.95)\n",
    "data[t+\"outlier_flag\"]=np.where(data[t]>x,1,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SeriousDlqin2yrs</th>\n",
       "      <th>RevolvingUtilizationOfUnsecuredLines</th>\n",
       "      <th>age</th>\n",
       "      <th>NumberOfTime30to59DaysPastDueNotWorse</th>\n",
       "      <th>DebtRatio</th>\n",
       "      <th>MonthlyIncome</th>\n",
       "      <th>NumberOfOpenCreditLinesAndLoans</th>\n",
       "      <th>NumberOfTimes90DaysLate</th>\n",
       "      <th>NumberRealEstateLoansOrLines</th>\n",
       "      <th>NumberOfTime60to89DaysPastDueNotWorse</th>\n",
       "      <th>NumberOfDependents</th>\n",
       "      <th>RevolvingUtilizationOfUnsecuredLinesoutlier_flag</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>1.500000e+05</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>0.066840</td>\n",
       "      <td>6.048438</td>\n",
       "      <td>52.295207</td>\n",
       "      <td>0.421033</td>\n",
       "      <td>353.005076</td>\n",
       "      <td>6.418455e+03</td>\n",
       "      <td>8.452760</td>\n",
       "      <td>0.265973</td>\n",
       "      <td>1.018240</td>\n",
       "      <td>0.240387</td>\n",
       "      <td>0.737413</td>\n",
       "      <td>0.022253</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.249746</td>\n",
       "      <td>249.755371</td>\n",
       "      <td>14.771866</td>\n",
       "      <td>4.192781</td>\n",
       "      <td>2037.818523</td>\n",
       "      <td>1.289040e+04</td>\n",
       "      <td>5.145951</td>\n",
       "      <td>4.169304</td>\n",
       "      <td>1.129771</td>\n",
       "      <td>4.155179</td>\n",
       "      <td>1.107021</td>\n",
       "      <td>0.147486</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.029867</td>\n",
       "      <td>41.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.175074</td>\n",
       "      <td>3.903000e+03</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.154181</td>\n",
       "      <td>52.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.366508</td>\n",
       "      <td>5.400000e+03</td>\n",
       "      <td>8.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.559046</td>\n",
       "      <td>63.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.868254</td>\n",
       "      <td>7.400000e+03</td>\n",
       "      <td>11.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>50708.000000</td>\n",
       "      <td>109.000000</td>\n",
       "      <td>98.000000</td>\n",
       "      <td>329664.000000</td>\n",
       "      <td>3.008750e+06</td>\n",
       "      <td>58.000000</td>\n",
       "      <td>98.000000</td>\n",
       "      <td>54.000000</td>\n",
       "      <td>98.000000</td>\n",
       "      <td>20.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       SeriousDlqin2yrs  RevolvingUtilizationOfUnsecuredLines            age  \\\n",
       "count     150000.000000                         150000.000000  150000.000000   \n",
       "mean           0.066840                              6.048438      52.295207   \n",
       "std            0.249746                            249.755371      14.771866   \n",
       "min            0.000000                              0.000000       0.000000   \n",
       "25%            0.000000                              0.029867      41.000000   \n",
       "50%            0.000000                              0.154181      52.000000   \n",
       "75%            0.000000                              0.559046      63.000000   \n",
       "max            1.000000                          50708.000000     109.000000   \n",
       "\n",
       "       NumberOfTime30to59DaysPastDueNotWorse      DebtRatio  MonthlyIncome  \\\n",
       "count                          150000.000000  150000.000000   1.500000e+05   \n",
       "mean                                0.421033     353.005076   6.418455e+03   \n",
       "std                                 4.192781    2037.818523   1.289040e+04   \n",
       "min                                 0.000000       0.000000   0.000000e+00   \n",
       "25%                                 0.000000       0.175074   3.903000e+03   \n",
       "50%                                 0.000000       0.366508   5.400000e+03   \n",
       "75%                                 0.000000       0.868254   7.400000e+03   \n",
       "max                                98.000000  329664.000000   3.008750e+06   \n",
       "\n",
       "       NumberOfOpenCreditLinesAndLoans  NumberOfTimes90DaysLate  \\\n",
       "count                    150000.000000            150000.000000   \n",
       "mean                          8.452760                 0.265973   \n",
       "std                           5.145951                 4.169304   \n",
       "min                           0.000000                 0.000000   \n",
       "25%                           5.000000                 0.000000   \n",
       "50%                           8.000000                 0.000000   \n",
       "75%                          11.000000                 0.000000   \n",
       "max                          58.000000                98.000000   \n",
       "\n",
       "       NumberRealEstateLoansOrLines  NumberOfTime60to89DaysPastDueNotWorse  \\\n",
       "count                 150000.000000                          150000.000000   \n",
       "mean                       1.018240                               0.240387   \n",
       "std                        1.129771                               4.155179   \n",
       "min                        0.000000                               0.000000   \n",
       "25%                        0.000000                               0.000000   \n",
       "50%                        1.000000                               0.000000   \n",
       "75%                        2.000000                               0.000000   \n",
       "max                       54.000000                              98.000000   \n",
       "\n",
       "       NumberOfDependents  RevolvingUtilizationOfUnsecuredLinesoutlier_flag  \n",
       "count       150000.000000                                     150000.000000  \n",
       "mean             0.737413                                          0.022253  \n",
       "std              1.107021                                          0.147486  \n",
       "min              0.000000                                          0.000000  \n",
       "25%              0.000000                                          0.000000  \n",
       "50%              0.000000                                          0.000000  \n",
       "75%              1.000000                                          0.000000  \n",
       "max             20.000000                                          1.000000  "
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for t in vars:\n",
    "    x=data[t].quantile(0.95)\n",
    "    data[t+\"outlier_flag\"]=np.where(data[t]>x,1,0)\n",
    "    data[t]=np.where(data[t]>x,x,data[t])\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SeriousDlqin2yrs</th>\n",
       "      <th>RevolvingUtilizationOfUnsecuredLines</th>\n",
       "      <th>age</th>\n",
       "      <th>NumberOfTime30to59DaysPastDueNotWorse</th>\n",
       "      <th>DebtRatio</th>\n",
       "      <th>MonthlyIncome</th>\n",
       "      <th>NumberOfOpenCreditLinesAndLoans</th>\n",
       "      <th>NumberOfTimes90DaysLate</th>\n",
       "      <th>NumberRealEstateLoansOrLines</th>\n",
       "      <th>NumberOfTime60to89DaysPastDueNotWorse</th>\n",
       "      <th>NumberOfDependents</th>\n",
       "      <th>RevolvingUtilizationOfUnsecuredLinesoutlier_flag</th>\n",
       "      <th>NumberOfTime30to59DaysPastDueNotWorseoutlier_flag</th>\n",
       "      <th>DebtRatiooutlier_flag</th>\n",
       "      <th>MonthlyIncomeoutlier_flag</th>\n",
       "      <th>NumberOfTimes90DaysLateoutlier_flag</th>\n",
       "      <th>NumberRealEstateLoansOrLinesoutlier_flag</th>\n",
       "      <th>NumberOfTime60to89DaysPastDueNotWorseoutlier_flag</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>0.066840</td>\n",
       "      <td>0.319196</td>\n",
       "      <td>52.295207</td>\n",
       "      <td>0.212873</td>\n",
       "      <td>254.635916</td>\n",
       "      <td>5924.638427</td>\n",
       "      <td>8.452760</td>\n",
       "      <td>0.055587</td>\n",
       "      <td>0.968253</td>\n",
       "      <td>0.050693</td>\n",
       "      <td>0.737413</td>\n",
       "      <td>0.022253</td>\n",
       "      <td>0.022340</td>\n",
       "      <td>0.049960</td>\n",
       "      <td>0.049780</td>\n",
       "      <td>0.020633</td>\n",
       "      <td>0.024347</td>\n",
       "      <td>0.012487</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.249746</td>\n",
       "      <td>0.349481</td>\n",
       "      <td>14.771866</td>\n",
       "      <td>0.523017</td>\n",
       "      <td>662.435683</td>\n",
       "      <td>3154.343174</td>\n",
       "      <td>5.145951</td>\n",
       "      <td>0.229123</td>\n",
       "      <td>0.921476</td>\n",
       "      <td>0.219371</td>\n",
       "      <td>1.107021</td>\n",
       "      <td>0.147486</td>\n",
       "      <td>0.147754</td>\n",
       "      <td>0.217752</td>\n",
       "      <td>0.217419</td>\n",
       "      <td>0.142153</td>\n",
       "      <td>0.154006</td>\n",
       "      <td>0.111007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.029867</td>\n",
       "      <td>41.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.175074</td>\n",
       "      <td>3903.000000</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.154181</td>\n",
       "      <td>52.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.366508</td>\n",
       "      <td>5400.000000</td>\n",
       "      <td>8.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.559046</td>\n",
       "      <td>63.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.868254</td>\n",
       "      <td>7400.000000</td>\n",
       "      <td>11.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>109.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>2449.000000</td>\n",
       "      <td>13500.000000</td>\n",
       "      <td>58.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>20.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       SeriousDlqin2yrs  RevolvingUtilizationOfUnsecuredLines            age  \\\n",
       "count     150000.000000                         150000.000000  150000.000000   \n",
       "mean           0.066840                              0.319196      52.295207   \n",
       "std            0.249746                              0.349481      14.771866   \n",
       "min            0.000000                              0.000000       0.000000   \n",
       "25%            0.000000                              0.029867      41.000000   \n",
       "50%            0.000000                              0.154181      52.000000   \n",
       "75%            0.000000                              0.559046      63.000000   \n",
       "max            1.000000                              1.000000     109.000000   \n",
       "\n",
       "       NumberOfTime30to59DaysPastDueNotWorse      DebtRatio  MonthlyIncome  \\\n",
       "count                          150000.000000  150000.000000  150000.000000   \n",
       "mean                                0.212873     254.635916    5924.638427   \n",
       "std                                 0.523017     662.435683    3154.343174   \n",
       "min                                 0.000000       0.000000       0.000000   \n",
       "25%                                 0.000000       0.175074    3903.000000   \n",
       "50%                                 0.000000       0.366508    5400.000000   \n",
       "75%                                 0.000000       0.868254    7400.000000   \n",
       "max                                 2.000000    2449.000000   13500.000000   \n",
       "\n",
       "       NumberOfOpenCreditLinesAndLoans  NumberOfTimes90DaysLate  \\\n",
       "count                    150000.000000            150000.000000   \n",
       "mean                          8.452760                 0.055587   \n",
       "std                           5.145951                 0.229123   \n",
       "min                           0.000000                 0.000000   \n",
       "25%                           5.000000                 0.000000   \n",
       "50%                           8.000000                 0.000000   \n",
       "75%                          11.000000                 0.000000   \n",
       "max                          58.000000                 1.000000   \n",
       "\n",
       "       NumberRealEstateLoansOrLines  NumberOfTime60to89DaysPastDueNotWorse  \\\n",
       "count                 150000.000000                          150000.000000   \n",
       "mean                       0.968253                               0.050693   \n",
       "std                        0.921476                               0.219371   \n",
       "min                        0.000000                               0.000000   \n",
       "25%                        0.000000                               0.000000   \n",
       "50%                        1.000000                               0.000000   \n",
       "75%                        2.000000                               0.000000   \n",
       "max                        3.000000                               1.000000   \n",
       "\n",
       "       NumberOfDependents  RevolvingUtilizationOfUnsecuredLinesoutlier_flag  \\\n",
       "count       150000.000000                                     150000.000000   \n",
       "mean             0.737413                                          0.022253   \n",
       "std              1.107021                                          0.147486   \n",
       "min              0.000000                                          0.000000   \n",
       "25%              0.000000                                          0.000000   \n",
       "50%              0.000000                                          0.000000   \n",
       "75%              1.000000                                          0.000000   \n",
       "max             20.000000                                          1.000000   \n",
       "\n",
       "       NumberOfTime30to59DaysPastDueNotWorseoutlier_flag  \\\n",
       "count                                      150000.000000   \n",
       "mean                                            0.022340   \n",
       "std                                             0.147754   \n",
       "min                                             0.000000   \n",
       "25%                                             0.000000   \n",
       "50%                                             0.000000   \n",
       "75%                                             0.000000   \n",
       "max                                             1.000000   \n",
       "\n",
       "       DebtRatiooutlier_flag  MonthlyIncomeoutlier_flag  \\\n",
       "count          150000.000000              150000.000000   \n",
       "mean                0.049960                   0.049780   \n",
       "std                 0.217752                   0.217419   \n",
       "min                 0.000000                   0.000000   \n",
       "25%                 0.000000                   0.000000   \n",
       "50%                 0.000000                   0.000000   \n",
       "75%                 0.000000                   0.000000   \n",
       "max                 1.000000                   1.000000   \n",
       "\n",
       "       NumberOfTimes90DaysLateoutlier_flag  \\\n",
       "count                        150000.000000   \n",
       "mean                              0.020633   \n",
       "std                               0.142153   \n",
       "min                               0.000000   \n",
       "25%                               0.000000   \n",
       "50%                               0.000000   \n",
       "75%                               0.000000   \n",
       "max                               1.000000   \n",
       "\n",
       "       NumberRealEstateLoansOrLinesoutlier_flag  \\\n",
       "count                             150000.000000   \n",
       "mean                                   0.024347   \n",
       "std                                    0.154006   \n",
       "min                                    0.000000   \n",
       "25%                                    0.000000   \n",
       "50%                                    0.000000   \n",
       "75%                                    0.000000   \n",
       "max                                    1.000000   \n",
       "\n",
       "       NumberOfTime60to89DaysPastDueNotWorseoutlier_flag  \n",
       "count                                      150000.000000  \n",
       "mean                                            0.012487  \n",
       "std                                             0.111007  \n",
       "min                                             0.000000  \n",
       "25%                                             0.000000  \n",
       "50%                                             0.000000  \n",
       "75%                                             0.000000  \n",
       "max                                             1.000000  "
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.00       0.000000\n",
       "0.05       0.004329\n",
       "0.10       0.030874\n",
       "0.15       0.086375\n",
       "0.20       0.133773\n",
       "0.25       0.175074\n",
       "0.30       0.213697\n",
       "0.35       0.250716\n",
       "0.40       0.287460\n",
       "0.45       0.324993\n",
       "0.50       0.366508\n",
       "0.55       0.412618\n",
       "0.60       0.467506\n",
       "0.65       0.538731\n",
       "0.70       0.649189\n",
       "0.75       0.868254\n",
       "0.80       4.000000\n",
       "0.85     269.150000\n",
       "0.90    1267.000000\n",
       "0.95    2449.000000\n",
       "Name: DebtRatio, dtype: float64"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['DebtRatio'].quantile(np.arange(0,1,0.05))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data['DebtRatio_newoutlier']=np.where(data['DebtRatio']>1,1,0)\n",
    "data['DebtRatio']=np.where(data['DebtRatio']>1,1,data['DebtRatio'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SeriousDlqin2yrs</th>\n",
       "      <th>RevolvingUtilizationOfUnsecuredLines</th>\n",
       "      <th>age</th>\n",
       "      <th>NumberOfTime30to59DaysPastDueNotWorse</th>\n",
       "      <th>DebtRatio</th>\n",
       "      <th>MonthlyIncome</th>\n",
       "      <th>NumberOfOpenCreditLinesAndLoans</th>\n",
       "      <th>NumberOfTimes90DaysLate</th>\n",
       "      <th>NumberRealEstateLoansOrLines</th>\n",
       "      <th>NumberOfTime60to89DaysPastDueNotWorse</th>\n",
       "      <th>NumberOfDependents</th>\n",
       "      <th>RevolvingUtilizationOfUnsecuredLinesoutlier_flag</th>\n",
       "      <th>NumberOfTime30to59DaysPastDueNotWorseoutlier_flag</th>\n",
       "      <th>DebtRatiooutlier_flag</th>\n",
       "      <th>MonthlyIncomeoutlier_flag</th>\n",
       "      <th>NumberOfTimes90DaysLateoutlier_flag</th>\n",
       "      <th>NumberRealEstateLoansOrLinesoutlier_flag</th>\n",
       "      <th>NumberOfTime60to89DaysPastDueNotWorseoutlier_flag</th>\n",
       "      <th>DebtRatio_newoutlier</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "      <td>150000.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>0.066840</td>\n",
       "      <td>0.319196</td>\n",
       "      <td>52.295207</td>\n",
       "      <td>0.212873</td>\n",
       "      <td>0.466287</td>\n",
       "      <td>5924.638427</td>\n",
       "      <td>8.452760</td>\n",
       "      <td>0.055587</td>\n",
       "      <td>0.968253</td>\n",
       "      <td>0.050693</td>\n",
       "      <td>0.737413</td>\n",
       "      <td>0.022253</td>\n",
       "      <td>0.022340</td>\n",
       "      <td>0.049960</td>\n",
       "      <td>0.049780</td>\n",
       "      <td>0.020633</td>\n",
       "      <td>0.024347</td>\n",
       "      <td>0.012487</td>\n",
       "      <td>0.234247</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.249746</td>\n",
       "      <td>0.349481</td>\n",
       "      <td>14.771866</td>\n",
       "      <td>0.523017</td>\n",
       "      <td>0.355455</td>\n",
       "      <td>3154.343174</td>\n",
       "      <td>5.145951</td>\n",
       "      <td>0.229123</td>\n",
       "      <td>0.921476</td>\n",
       "      <td>0.219371</td>\n",
       "      <td>1.107021</td>\n",
       "      <td>0.147486</td>\n",
       "      <td>0.147754</td>\n",
       "      <td>0.217752</td>\n",
       "      <td>0.217419</td>\n",
       "      <td>0.142153</td>\n",
       "      <td>0.154006</td>\n",
       "      <td>0.111007</td>\n",
       "      <td>0.423312</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.029867</td>\n",
       "      <td>41.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.175074</td>\n",
       "      <td>3903.000000</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.154181</td>\n",
       "      <td>52.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.366508</td>\n",
       "      <td>5400.000000</td>\n",
       "      <td>8.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.559046</td>\n",
       "      <td>63.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.868254</td>\n",
       "      <td>7400.000000</td>\n",
       "      <td>11.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>109.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>13500.000000</td>\n",
       "      <td>58.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>20.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       SeriousDlqin2yrs  RevolvingUtilizationOfUnsecuredLines            age  \\\n",
       "count     150000.000000                         150000.000000  150000.000000   \n",
       "mean           0.066840                              0.319196      52.295207   \n",
       "std            0.249746                              0.349481      14.771866   \n",
       "min            0.000000                              0.000000       0.000000   \n",
       "25%            0.000000                              0.029867      41.000000   \n",
       "50%            0.000000                              0.154181      52.000000   \n",
       "75%            0.000000                              0.559046      63.000000   \n",
       "max            1.000000                              1.000000     109.000000   \n",
       "\n",
       "       NumberOfTime30to59DaysPastDueNotWorse      DebtRatio  MonthlyIncome  \\\n",
       "count                          150000.000000  150000.000000  150000.000000   \n",
       "mean                                0.212873       0.466287    5924.638427   \n",
       "std                                 0.523017       0.355455    3154.343174   \n",
       "min                                 0.000000       0.000000       0.000000   \n",
       "25%                                 0.000000       0.175074    3903.000000   \n",
       "50%                                 0.000000       0.366508    5400.000000   \n",
       "75%                                 0.000000       0.868254    7400.000000   \n",
       "max                                 2.000000       1.000000   13500.000000   \n",
       "\n",
       "       NumberOfOpenCreditLinesAndLoans  NumberOfTimes90DaysLate  \\\n",
       "count                    150000.000000            150000.000000   \n",
       "mean                          8.452760                 0.055587   \n",
       "std                           5.145951                 0.229123   \n",
       "min                           0.000000                 0.000000   \n",
       "25%                           5.000000                 0.000000   \n",
       "50%                           8.000000                 0.000000   \n",
       "75%                          11.000000                 0.000000   \n",
       "max                          58.000000                 1.000000   \n",
       "\n",
       "       NumberRealEstateLoansOrLines  NumberOfTime60to89DaysPastDueNotWorse  \\\n",
       "count                 150000.000000                          150000.000000   \n",
       "mean                       0.968253                               0.050693   \n",
       "std                        0.921476                               0.219371   \n",
       "min                        0.000000                               0.000000   \n",
       "25%                        0.000000                               0.000000   \n",
       "50%                        1.000000                               0.000000   \n",
       "75%                        2.000000                               0.000000   \n",
       "max                        3.000000                               1.000000   \n",
       "\n",
       "       NumberOfDependents  RevolvingUtilizationOfUnsecuredLinesoutlier_flag  \\\n",
       "count       150000.000000                                     150000.000000   \n",
       "mean             0.737413                                          0.022253   \n",
       "std              1.107021                                          0.147486   \n",
       "min              0.000000                                          0.000000   \n",
       "25%              0.000000                                          0.000000   \n",
       "50%              0.000000                                          0.000000   \n",
       "75%              1.000000                                          0.000000   \n",
       "max             20.000000                                          1.000000   \n",
       "\n",
       "       NumberOfTime30to59DaysPastDueNotWorseoutlier_flag  \\\n",
       "count                                      150000.000000   \n",
       "mean                                            0.022340   \n",
       "std                                             0.147754   \n",
       "min                                             0.000000   \n",
       "25%                                             0.000000   \n",
       "50%                                             0.000000   \n",
       "75%                                             0.000000   \n",
       "max                                             1.000000   \n",
       "\n",
       "       DebtRatiooutlier_flag  MonthlyIncomeoutlier_flag  \\\n",
       "count          150000.000000              150000.000000   \n",
       "mean                0.049960                   0.049780   \n",
       "std                 0.217752                   0.217419   \n",
       "min                 0.000000                   0.000000   \n",
       "25%                 0.000000                   0.000000   \n",
       "50%                 0.000000                   0.000000   \n",
       "75%                 0.000000                   0.000000   \n",
       "max                 1.000000                   1.000000   \n",
       "\n",
       "       NumberOfTimes90DaysLateoutlier_flag  \\\n",
       "count                        150000.000000   \n",
       "mean                              0.020633   \n",
       "std                               0.142153   \n",
       "min                               0.000000   \n",
       "25%                               0.000000   \n",
       "50%                               0.000000   \n",
       "75%                               0.000000   \n",
       "max                               1.000000   \n",
       "\n",
       "       NumberRealEstateLoansOrLinesoutlier_flag  \\\n",
       "count                             150000.000000   \n",
       "mean                                   0.024347   \n",
       "std                                    0.154006   \n",
       "min                                    0.000000   \n",
       "25%                                    0.000000   \n",
       "50%                                    0.000000   \n",
       "75%                                    0.000000   \n",
       "max                                    1.000000   \n",
       "\n",
       "       NumberOfTime60to89DaysPastDueNotWorseoutlier_flag  DebtRatio_newoutlier  \n",
       "count                                      150000.000000         150000.000000  \n",
       "mean                                            0.012487              0.234247  \n",
       "std                                             0.111007              0.423312  \n",
       "min                                             0.000000              0.000000  \n",
       "25%                                             0.000000              0.000000  \n",
       "50%                                             0.000000              0.000000  \n",
       "75%                                             0.000000              0.000000  \n",
       "max                                             1.000000              1.000000  "
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Admin\\Anaconda4\\lib\\site-packages\\ipykernel\\__main__.py:2: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future\n",
      "  from ipykernel import kernelapp as app\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "sample=np.random.randint(0,data.shape[0],np.around(data.shape[0]*0.8))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train=data.loc[sample]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "test=data.drop(data.index[sample])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# importing the package for logistic regression\n",
    "import statsmodels.formula.api as smf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['SeriousDlqin2yrs', 'RevolvingUtilizationOfUnsecuredLines', 'age',\n",
       "       'NumberOfTime30to59DaysPastDueNotWorse', 'DebtRatio', 'MonthlyIncome',\n",
       "       'NumberOfOpenCreditLinesAndLoans', 'NumberOfTimes90DaysLate',\n",
       "       'NumberRealEstateLoansOrLines', 'NumberOfTime60to89DaysPastDueNotWorse',\n",
       "       'NumberOfDependents',\n",
       "       'RevolvingUtilizationOfUnsecuredLinesoutlier_flag',\n",
       "       'NumberOfTime30to59DaysPastDueNotWorseoutlier_flag',\n",
       "       'DebtRatiooutlier_flag', 'MonthlyIncomeoutlier_flag',\n",
       "       'NumberOfTimes90DaysLateoutlier_flag',\n",
       "       'NumberRealEstateLoansOrLinesoutlier_flag',\n",
       "       'NumberOfTime60to89DaysPastDueNotWorseoutlier_flag',\n",
       "       'DebtRatio_newoutlier'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "est = smf.logit(formula='SeriousDlqin2yrs~RevolvingUtilizationOfUnsecuredLinesoutlier_flag+NumberOfTime30to59DaysPastDueNotWorseoutlier_flag+DebtRatiooutlier_flag+MonthlyIncomeoutlier_flag+NumberOfTimes90DaysLateoutlier_flag+NumberRealEstateLoansOrLinesoutlier_flag+RevolvingUtilizationOfUnsecuredLines+age+NumberOfTime30to59DaysPastDueNotWorse+DebtRatio+MonthlyIncome+MonthlyIncome+       NumberOfOpenCreditLinesAndLoans+ NumberOfTimes90DaysLate+       NumberRealEstateLoansOrLines+ NumberOfTime60to89DaysPastDueNotWorse+       NumberOfDependents+DebtRatio_newoutlier+NumberOfTime60to89DaysPastDueNotWorseoutlier_flag',data=train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimization terminated successfully.\n",
      "         Current function value: 0.181897\n",
      "         Iterations 8\n"
     ]
    }
   ],
   "source": [
    "est2=est.fit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                           Logit Regression Results                           \n",
      "==============================================================================\n",
      "Dep. Variable:       SeriousDlqin2yrs   No. Observations:               120000\n",
      "Model:                          Logit   Df Residuals:                   119981\n",
      "Method:                           MLE   Df Model:                           18\n",
      "Date:                Sun, 24 Sep 2017   Pseudo R-squ.:                  0.2564\n",
      "Time:                        16:57:43   Log-Likelihood:                -21828.\n",
      "converged:                       True   LL-Null:                       -29352.\n",
      "                                        LLR p-value:                     0.000\n",
      "=====================================================================================================================\n",
      "                                                        coef    std err          z      P>|z|      [95.0% Conf. Int.]\n",
      "---------------------------------------------------------------------------------------------------------------------\n",
      "Intercept                                            -3.5855      0.068    -52.957      0.000        -3.718    -3.453\n",
      "RevolvingUtilizationOfUnsecuredLinesoutlier_flag      0.3880      0.051      7.542      0.000         0.287     0.489\n",
      "NumberOfTime30to59DaysPastDueNotWorseoutlier_flag    -0.0869      0.060     -1.459      0.145        -0.204     0.030\n",
      "DebtRatiooutlier_flag                                -0.3083      0.072     -4.266      0.000        -0.450    -0.167\n",
      "MonthlyIncomeoutlier_flag                            -0.0854      0.083     -1.025      0.305        -0.249     0.078\n",
      "NumberOfTimes90DaysLateoutlier_flag                   0.3954      0.060      6.596      0.000         0.278     0.513\n",
      "NumberRealEstateLoansOrLinesoutlier_flag              0.7250      0.077      9.367      0.000         0.573     0.877\n",
      "RevolvingUtilizationOfUnsecuredLines                  1.7811      0.041     42.997      0.000         1.700     1.862\n",
      "age                                                  -0.0152      0.001    -14.300      0.000        -0.017    -0.013\n",
      "NumberOfTime30to59DaysPastDueNotWorse                 0.6501      0.021     30.891      0.000         0.609     0.691\n",
      "DebtRatio                                             0.3083      0.077      3.979      0.000         0.156     0.460\n",
      "MonthlyIncome                                     -4.318e-05   5.87e-06     -7.354      0.000     -5.47e-05 -3.17e-05\n",
      "NumberOfOpenCreditLinesAndLoans                       0.0371      0.003     12.459      0.000         0.031     0.043\n",
      "NumberOfTimes90DaysLate                               1.3394      0.042     32.116      0.000         1.258     1.421\n",
      "NumberRealEstateLoansOrLines                          0.0052      0.020      0.256      0.798        -0.035     0.045\n",
      "NumberOfTime60to89DaysPastDueNotWorse                 1.0315      0.041     24.890      0.000         0.950     1.113\n",
      "NumberOfDependents                                    0.0525      0.012      4.560      0.000         0.030     0.075\n",
      "DebtRatio_newoutlier                                 -0.0891      0.061     -1.457      0.145        -0.209     0.031\n",
      "NumberOfTime60to89DaysPastDueNotWorseoutlier_flag    -0.0528      0.072     -0.731      0.465        -0.194     0.089\n",
      "=====================================================================================================================\n"
     ]
    }
   ],
   "source": [
    "print(est2.summary())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pred=est2.predict(test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.39536634,  0.02733288,  0.0147893 , ...,  0.10548374,\n",
       "        0.01090446,  0.02459089])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pred"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "test['prediction']=pred"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SeriousDlqin2yrs</th>\n",
       "      <th>RevolvingUtilizationOfUnsecuredLines</th>\n",
       "      <th>age</th>\n",
       "      <th>NumberOfTime30to59DaysPastDueNotWorse</th>\n",
       "      <th>DebtRatio</th>\n",
       "      <th>MonthlyIncome</th>\n",
       "      <th>NumberOfOpenCreditLinesAndLoans</th>\n",
       "      <th>NumberOfTimes90DaysLate</th>\n",
       "      <th>NumberRealEstateLoansOrLines</th>\n",
       "      <th>NumberOfTime60to89DaysPastDueNotWorse</th>\n",
       "      <th>NumberOfDependents</th>\n",
       "      <th>RevolvingUtilizationOfUnsecuredLinesoutlier_flag</th>\n",
       "      <th>NumberOfTime30to59DaysPastDueNotWorseoutlier_flag</th>\n",
       "      <th>DebtRatiooutlier_flag</th>\n",
       "      <th>MonthlyIncomeoutlier_flag</th>\n",
       "      <th>NumberOfTimes90DaysLateoutlier_flag</th>\n",
       "      <th>NumberRealEstateLoansOrLinesoutlier_flag</th>\n",
       "      <th>NumberOfTime60to89DaysPastDueNotWorseoutlier_flag</th>\n",
       "      <th>DebtRatio_newoutlier</th>\n",
       "      <th>prediction</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.766127</td>\n",
       "      <td>45</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.802982</td>\n",
       "      <td>9120.0</td>\n",
       "      <td>13</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>0.233810</td>\n",
       "      <td>30</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.036050</td>\n",
       "      <td>3300.0</td>\n",
       "      <td>5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0</td>\n",
       "      <td>0.213179</td>\n",
       "      <td>74</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.375607</td>\n",
       "      <td>3500.0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0</td>\n",
       "      <td>0.754464</td>\n",
       "      <td>39</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.209940</td>\n",
       "      <td>3500.0</td>\n",
       "      <td>8</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0</td>\n",
       "      <td>0.116951</td>\n",
       "      <td>27</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>5400.0</td>\n",
       "      <td>2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.02</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   SeriousDlqin2yrs  RevolvingUtilizationOfUnsecuredLines  age  \\\n",
       "0                 1                              0.766127   45   \n",
       "3                 0                              0.233810   30   \n",
       "5                 0                              0.213179   74   \n",
       "7                 0                              0.754464   39   \n",
       "8                 0                              0.116951   27   \n",
       "\n",
       "   NumberOfTime30to59DaysPastDueNotWorse  DebtRatio  MonthlyIncome  \\\n",
       "0                                    2.0   0.802982         9120.0   \n",
       "3                                    0.0   0.036050         3300.0   \n",
       "5                                    0.0   0.375607         3500.0   \n",
       "7                                    0.0   0.209940         3500.0   \n",
       "8                                    0.0   1.000000         5400.0   \n",
       "\n",
       "   NumberOfOpenCreditLinesAndLoans  NumberOfTimes90DaysLate  \\\n",
       "0                               13                      0.0   \n",
       "3                                5                      0.0   \n",
       "5                                3                      0.0   \n",
       "7                                8                      0.0   \n",
       "8                                2                      0.0   \n",
       "\n",
       "   NumberRealEstateLoansOrLines  NumberOfTime60to89DaysPastDueNotWorse  \\\n",
       "0                           3.0                                    0.0   \n",
       "3                           0.0                                    0.0   \n",
       "5                           1.0                                    0.0   \n",
       "7                           0.0                                    0.0   \n",
       "8                           0.0                                    0.0   \n",
       "\n",
       "   NumberOfDependents  RevolvingUtilizationOfUnsecuredLinesoutlier_flag  \\\n",
       "0                 2.0                                                 0   \n",
       "3                 0.0                                                 0   \n",
       "5                 1.0                                                 0   \n",
       "7                 0.0                                                 0   \n",
       "8                 0.0                                                 0   \n",
       "\n",
       "   NumberOfTime30to59DaysPastDueNotWorseoutlier_flag  DebtRatiooutlier_flag  \\\n",
       "0                                                  0                      0   \n",
       "3                                                  0                      0   \n",
       "5                                                  0                      0   \n",
       "7                                                  0                      0   \n",
       "8                                                  0                      0   \n",
       "\n",
       "   MonthlyIncomeoutlier_flag  NumberOfTimes90DaysLateoutlier_flag  \\\n",
       "0                          0                                    0   \n",
       "3                          0                                    0   \n",
       "5                          0                                    0   \n",
       "7                          0                                    0   \n",
       "8                          0                                    0   \n",
       "\n",
       "   NumberRealEstateLoansOrLinesoutlier_flag  \\\n",
       "0                                         1   \n",
       "3                                         0   \n",
       "5                                         0   \n",
       "7                                         0   \n",
       "8                                         0   \n",
       "\n",
       "   NumberOfTime60to89DaysPastDueNotWorseoutlier_flag  DebtRatio_newoutlier  \\\n",
       "0                                                  0                     0   \n",
       "3                                                  0                     0   \n",
       "5                                                  0                     0   \n",
       "7                                                  0                     0   \n",
       "8                                                  0                     1   \n",
       "\n",
       "   prediction  \n",
       "0        0.40  \n",
       "3        0.03  \n",
       "5        0.01  \n",
       "7        0.07  \n",
       "8        0.02  "
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test=test.round({'prediction':2})\n",
    "test.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                            prediction                                  \n",
      "                mean_predicted_default total_defaults total_observations\n",
      "prediction_rank                                                         \n",
      "1                             0.009979          67.33             6747.0\n",
      "2                             0.010000          67.47             6747.0\n",
      "3                             0.016652         112.35             6747.0\n",
      "4                             0.020000         134.94             6747.0\n",
      "5                             0.021177         142.88             6747.0\n",
      "6                             0.030000         202.41             6747.0\n",
      "7                             0.042205         284.76             6747.0\n",
      "8                             0.061829         417.16             6747.0\n",
      "9                             0.101420         684.28             6747.0\n",
      "10                            0.361833        2441.29             6747.0\n"
     ]
    }
   ],
   "source": [
    "test['prediction_rank']=pd.qcut(test['prediction'].rank(method='first').values, 10).codes + 1 \n",
    "test['prediction_rank'].describe()\n",
    "print(test.groupby(\"prediction_rank\").agg({\"prediction\": [np.mean, np.sum,len]}).rename(columns={'mean':'mean_predicted_default','sum':'total_defaults','len':'total_observations'}).to_string())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SeriousDlqin2yrs</th>\n",
       "      <th>RevolvingUtilizationOfUnsecuredLines</th>\n",
       "      <th>age</th>\n",
       "      <th>NumberOfTime30to59DaysPastDueNotWorse</th>\n",
       "      <th>DebtRatio</th>\n",
       "      <th>MonthlyIncome</th>\n",
       "      <th>NumberOfOpenCreditLinesAndLoans</th>\n",
       "      <th>NumberOfTimes90DaysLate</th>\n",
       "      <th>NumberRealEstateLoansOrLines</th>\n",
       "      <th>NumberOfTime60to89DaysPastDueNotWorse</th>\n",
       "      <th>...</th>\n",
       "      <th>RevolvingUtilizationOfUnsecuredLinesoutlier_flag</th>\n",
       "      <th>NumberOfTime30to59DaysPastDueNotWorseoutlier_flag</th>\n",
       "      <th>DebtRatiooutlier_flag</th>\n",
       "      <th>MonthlyIncomeoutlier_flag</th>\n",
       "      <th>NumberOfTimes90DaysLateoutlier_flag</th>\n",
       "      <th>NumberRealEstateLoansOrLinesoutlier_flag</th>\n",
       "      <th>NumberOfTime60to89DaysPastDueNotWorseoutlier_flag</th>\n",
       "      <th>DebtRatio_newoutlier</th>\n",
       "      <th>prediction</th>\n",
       "      <th>prediction_rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.766127</td>\n",
       "      <td>45</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.802982</td>\n",
       "      <td>9120.0</td>\n",
       "      <td>13</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.40</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>0.233810</td>\n",
       "      <td>30</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.036050</td>\n",
       "      <td>3300.0</td>\n",
       "      <td>5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.03</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0</td>\n",
       "      <td>0.213179</td>\n",
       "      <td>74</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.375607</td>\n",
       "      <td>3500.0</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.01</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0</td>\n",
       "      <td>0.754464</td>\n",
       "      <td>39</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.209940</td>\n",
       "      <td>3500.0</td>\n",
       "      <td>8</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.07</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0</td>\n",
       "      <td>0.116951</td>\n",
       "      <td>27</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>5400.0</td>\n",
       "      <td>2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.02</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 21 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   SeriousDlqin2yrs  RevolvingUtilizationOfUnsecuredLines  age  \\\n",
       "0                 1                              0.766127   45   \n",
       "3                 0                              0.233810   30   \n",
       "5                 0                              0.213179   74   \n",
       "7                 0                              0.754464   39   \n",
       "8                 0                              0.116951   27   \n",
       "\n",
       "   NumberOfTime30to59DaysPastDueNotWorse  DebtRatio  MonthlyIncome  \\\n",
       "0                                    2.0   0.802982         9120.0   \n",
       "3                                    0.0   0.036050         3300.0   \n",
       "5                                    0.0   0.375607         3500.0   \n",
       "7                                    0.0   0.209940         3500.0   \n",
       "8                                    0.0   1.000000         5400.0   \n",
       "\n",
       "   NumberOfOpenCreditLinesAndLoans  NumberOfTimes90DaysLate  \\\n",
       "0                               13                      0.0   \n",
       "3                                5                      0.0   \n",
       "5                                3                      0.0   \n",
       "7                                8                      0.0   \n",
       "8                                2                      0.0   \n",
       "\n",
       "   NumberRealEstateLoansOrLines  NumberOfTime60to89DaysPastDueNotWorse  \\\n",
       "0                           3.0                                    0.0   \n",
       "3                           0.0                                    0.0   \n",
       "5                           1.0                                    0.0   \n",
       "7                           0.0                                    0.0   \n",
       "8                           0.0                                    0.0   \n",
       "\n",
       "        ...         RevolvingUtilizationOfUnsecuredLinesoutlier_flag  \\\n",
       "0       ...                                                        0   \n",
       "3       ...                                                        0   \n",
       "5       ...                                                        0   \n",
       "7       ...                                                        0   \n",
       "8       ...                                                        0   \n",
       "\n",
       "   NumberOfTime30to59DaysPastDueNotWorseoutlier_flag  DebtRatiooutlier_flag  \\\n",
       "0                                                  0                      0   \n",
       "3                                                  0                      0   \n",
       "5                                                  0                      0   \n",
       "7                                                  0                      0   \n",
       "8                                                  0                      0   \n",
       "\n",
       "   MonthlyIncomeoutlier_flag  NumberOfTimes90DaysLateoutlier_flag  \\\n",
       "0                          0                                    0   \n",
       "3                          0                                    0   \n",
       "5                          0                                    0   \n",
       "7                          0                                    0   \n",
       "8                          0                                    0   \n",
       "\n",
       "   NumberRealEstateLoansOrLinesoutlier_flag  \\\n",
       "0                                         1   \n",
       "3                                         0   \n",
       "5                                         0   \n",
       "7                                         0   \n",
       "8                                         0   \n",
       "\n",
       "   NumberOfTime60to89DaysPastDueNotWorseoutlier_flag  DebtRatio_newoutlier  \\\n",
       "0                                                  0                     0   \n",
       "3                                                  0                     0   \n",
       "5                                                  0                     0   \n",
       "7                                                  0                     0   \n",
       "8                                                  0                     1   \n",
       "\n",
       "   prediction  prediction_rank  \n",
       "0        0.40               10  \n",
       "3        0.03                5  \n",
       "5        0.01                1  \n",
       "7        0.07                8  \n",
       "8        0.02                3  \n",
       "\n",
       "[5 rows x 21 columns]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.06703720171928264"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test['SeriousDlqin2yrs'].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.85676367009616383"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.metrics import roc_auc_score\n",
    "y_true = np.array(test['SeriousDlqin2yrs'])\n",
    "y_scores = np.array(test['prediction'])\n",
    "roc_auc_score(y_true, y_scores)"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
